import sys
import os.path

extensions_to_fix = []
extensions_to_fix.append(".cpp")
extensions_to_fix.append(".hpp")
extensions_to_fix.append(".c")
extensions_to_fix.append(".h")
extensions_to_fix.append(".xml")
extensions_to_fix.append(".buildng")
extensions_to_fix.append(".py")
extensions_to_fix.append(".sh")

def fix_file(name):
    in_file = open(name,"r")
    lines = in_file.readlines()
    in_file.close()

    for i in range(len(lines)):
        lines[i] = lines[i].replace("\r\n","\n")

    out_file = open(name,"wb");
    for line in lines:
        out_file.write(line)
    out_file.close();

def fix_files(arg,dirname,names):
    for name in names:
        full_name = os.path.join(dirname,name)
        if os.path.isdir(full_name):
            if name.startswith("."):
                names.remove(name)
        if not os.path.isfile(full_name):
            continue
        
        for extension in extensions_to_fix:
            if name.endswith(extension):
                fix_file(full_name)
                break

if len(sys.argv) == 2:
    root_dir = os.path.join(os.getcwd().curdir,sys.argv[1])
elif len(sys.argv) == 1:
    root_dir = os.getcwd()
else:
    raise Exception("Too many arguments specified")

os.path.walk(root_dir,fix_files,None)
